package com.hxzy.module.admin.controller;

import com.github.pagehelper.PageInfo;
import com.hxzy.framework.common.exception.ApiException;
import com.hxzy.framework.common.resp.R;
import com.hxzy.framework.common.resp.ResultCode;
import com.hxzy.module.admin.api.role.req.*;
import com.hxzy.module.admin.api.role.resp.AdminRoleResp;
import com.hxzy.module.admin.api.role.resp.RoleAuthorityTreeResp;
import com.hxzy.module.admin.entity.AdminRole;
import com.hxzy.module.admin.mapstruct.AdminMapStruct;
import com.hxzy.module.admin.service.AdminRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author wy
 * @Description 角色管理Api
 * @Date 2024/1/31 11:38
 * @Version 1.0
 */
@Api(tags = "后台用户-角色管理Api")
@RestController
@RequestMapping("/ums")
public class AdminRoleController {

    @Autowired
    private AdminRoleService adminRoleService;

    @ApiOperation(value = "角色列表", notes = "角色列表")
    @GetMapping(value = "/roleList")
    public R<PageInfo<AdminRoleResp>> roleList(@Validated   AdminRoleReq req) {
        return R.success(adminRoleService.roleList(req));
    }

    @ApiOperation(value = "角色详情", notes = "角色详情")
    @GetMapping(value = "/roleDetail/{roleId}")
    public R<AdminRoleResp> roleDetail(@PathVariable(value = "roleId") Long roleId){
        if(Objects.isNull(roleId)){
            throw new ApiException(ResultCode.MISSING_REQUEST_PARAM_ERROR.getCode(),"角色编号不能为空!");
        }

        AdminRole adminRole = this.adminRoleService.getById(roleId);

        if(Objects.isNull(adminRole)){
            return R.failed(ResultCode.DATA_NOT_FOUND);
        }
        return R.success(AdminMapStruct.INSTANCE.convert(adminRole));
    }

    @ApiOperation(value = "角色权限树", notes = "角色权限树")
    @GetMapping(value = "/roleAuthorityTree/{roleId}")
    public R<List<RoleAuthorityTreeResp>> roleAuthorityTree( @PathVariable(value = "roleId") Long roleId) {
        if(Objects.isNull(roleId)){
            throw new ApiException(ResultCode.MISSING_REQUEST_PARAM_ERROR.getCode(),"角色编号不能为空!");
        }
        return R.success(this.adminRoleService.roleAuthorityTree(roleId));
    }

    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping(value = "/addRole")
    public R addRole(@RequestBody @Valid AdminRoleAddReq req){
       boolean result=this.adminRoleService.add(req);
       return result? R.success(): R.failed(ResultCode.FAILED);
    }

    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PutMapping(value = "/editRole")
    public R editRole(@RequestBody @Valid AdminRoleEditReq req){
        boolean result=this.adminRoleService.editRole(req);
        return result? R.success(): R.failed(ResultCode.FAILED);
    }


    @ApiOperation(value = "修改角色权限", notes = "修改角色权限")
    @PutMapping(value = "/changeRoleAuthority")
    public R changeRoleAuthority(@Validated @RequestBody ChangeRoleAuthorityReq req) {
       boolean result=this.adminRoleService.changeRoleAuthority(req);
       return result? R.success(): R.failed(ResultCode.FAILED);
    }

    @ApiOperation(value = "修改角色状态", notes = "修改角色状态")
    @PutMapping(value = "/changeRoleState")
    public R  changeRoleState(@RequestBody @Validated ChangeRoleStateReq req) {
        boolean result=this.adminRoleService.changeRoleState(req);
        return result? R.success(): R.failed(ResultCode.FAILED);
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @DeleteMapping(value = "/delRole/{roleId}")
    public R  delRole(@PathVariable(value = "roleId") Long roleId) {
        if(Objects.isNull(roleId)){
            throw new ApiException(ResultCode.MISSING_REQUEST_PARAM_ERROR.getCode(),"角色编号不能为空!");
        }
        boolean result=this.adminRoleService.delRole(roleId);
        return result? R.success(): R.failed(ResultCode.FAILED);
    }

}
